package com.hzit.mmcb.core.controller.api;


import com.hzit.common.result.R;
import com.hzit.mmb.base.utils.JwtUtils;
import com.hzit.mmcb.core.hfb.RequestHelper;
import com.hzit.mmcb.core.service.UserAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.rmi.MarshalledObject;
import java.util.Map;

/**
 * <p>
 * 用户账户 前端控制器
 * </p>
 *
 * @author ljh
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/api/core/userAccount")
@Api(tags = "用户余额")
public class ApiUserAccountController {

    @Autowired
    private UserAccountService userAccountService;

    /**
     * 充值接口
     * @param chargeAmt
     * @param token
     * @return
     */
    @GetMapping("/auth/commitCharge/{chargeAmt}")
    @ApiOperation("充值")
    public R commitCharge(@PathVariable BigDecimal chargeAmt,@RequestHeader("token") String token){
        Long userId = JwtUtils.getUserId(token);
        String formStr=userAccountService.commitCharge(chargeAmt,userId);
        return R.ok().data("formStr",formStr);
    }

    @PostMapping("/notify")
    @ApiOperation("充值异步回调接口")
    public String notify(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = RequestHelper.switchMap(parameterMap);
        //判断验签是否成功
        if (RequestHelper.isSignEquals(map)){
            //判断状态是否是充值成功
            if ("0001".equals(map.get("resultCode"))) {
                String s = userAccountService.notifyAccount(map);
                return s;
            }
        }
        return "fail";
    }


    /**
     * 查询用户余额
     * @param token
     * @return
     */
    @GetMapping("/getAccount")
    @ApiOperation("查询用户余额")
    public R getAccount(@RequestHeader("token") String token){
        Long userId = JwtUtils.getUserId(token);
        BigDecimal account=userAccountService.getAccount(userId);
        return R.ok().data("account",account);
    }

    /**
     * 计算预期收益
     * @param investAmount      投资金额
     * @param lendYearRate      年利率
     * @param period            投资期数
     * @param returnMethod      还款方式
     * @return
     */
    @GetMapping("/getInterestCount/{investAmount}/{lendYearRate}/{period}/{returnMethod}")
    @ApiOperation("计算收益")
    public R getInterestCount(@PathVariable BigDecimal investAmount,
                              @PathVariable BigDecimal lendYearRate,
                              @PathVariable Integer period,
                              @PathVariable Integer returnMethod){

        BigDecimal interestCount=userAccountService.getInterestCount(investAmount,lendYearRate,period,returnMethod);
        return R.ok().data("interestCount",interestCount);
    }

    /**
     * 提现
     */
    @PostMapping("/auth/commitWithdraw/{fetchAmt}")
    @ApiOperation("提现")
    public R commitWithdraw(@PathVariable BigDecimal fetchAmt,@RequestHeader("token") String token){
        Long userId = JwtUtils.getUserId(token);
        String formStr=userAccountService.commitWithdraw(fetchAmt,userId);
        return R.ok().data("formStr",formStr);
    }

    /**
     * 提现回调
     */
    @PostMapping("/notifyWithdraw")
    @ApiOperation("提现异步回调地址")
    public String notifyWithdraw(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> map = RequestHelper.switchMap(parameterMap);
        //判断验签是否成功
        if (RequestHelper.isSignEquals(map)){
            //判断状态是否是充值成功
            if ("0001".equals(map.get("resultCode"))) {
                String s = userAccountService.notifyWithdraw(map);
                return s;
            }
        }
        return "fail";
    }

}

